package org.exmaple.springframework.beans.factory.support;

import org.exmaple.springframework.beans.factory.config.BeanDefinition;
import org.exmaple.springframework.beans.BeansException;
import org.exmaple.springframework.beans.factory.BeanFactory;

import java.util.Objects;

/**
 * @author DeYou
 * @date 2022/10/26
 */
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {
    @Override
    public Object getBean(String name) throws BeansException {
        Object bean = getSingleton( name );
        if(!Objects.isNull(bean)){
            return bean;
        }
        BeanDefinition beanDefinition = getBeanDefinition(name);
        return createBean(name, beanDefinition);
    }

    protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException;

    protected abstract Object createBean(String name, BeanDefinition beanDefinition) throws BeansException;


}